﻿package org.tbyrne.motion{
	import fl.transitions.TweenEvent;
	
	import flash.display.DisplayObject;
	import flash.filters.BlurFilter;
	import flash.geom.Rectangle;
	
	public class MotionTween extends MultiTween{
		private var _blurAmount:Number = 1;
		private var lastBounds:Rectangle;
		public var quality:Number = 3;
		public var filter:BlurFilter;
		
		function MotionTween(target:DisplayObject, properties:Array, easing:Function, startValues:Array, endValues:Array, duration:Number, useSeconds:Boolean, blurAmount:Number = 1){
			super(target, properties, easing, startValues, endValues, duration, useSeconds);
			target.visible = false;
			_blurAmount = blurAmount;
			
		}
		override public function setPosition(p:Number):void{
			super.setPosition(p);
			if(_target){
				if(filter==null){
					filter = new BlurFilter(0,0,quality);
				}
				var spriteTarget:DisplayObject = (_target as DisplayObject);
				if(lastBounds==null)lastBounds = spriteTarget.getBounds(_target.parent);
				var newBounds:Rectangle = spriteTarget.getBounds(_target.parent);
				filter.blurX = Math.abs(Math.max(newBounds.left-lastBounds.left,newBounds.right-lastBounds.right))*_blurAmount;
				filter.blurY = Math.abs(Math.max(newBounds.top-lastBounds.top,newBounds.bottom-lastBounds.bottom))*_blurAmount;
				filter.quality = quality;
				lastBounds = newBounds;
				_target.filters = [filter];
				_target.visible = true;
			}
		}
		override protected function dispatchStop(e:fl.transitions.TweenEvent):void{
			_target.filters = [];
			super.dispatchStop(e);
		}
		override protected function dispatchFinish(e:fl.transitions.TweenEvent):void{
			_target.filters = [];
			super.dispatchFinish(e);
		}
	}
}